package com.mrent.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.mrent.entity.AdminPayment;
import com.mrent.entity.Member;

@Component
public class AdminPaymentServiceImpl implements AdminPaymentService {
	
	
	
	/**
	 * 保存支付方式.
	 * 
	 * @param member
	 */
	public void savePayment(AdminPayment adminPayment){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try {
			trans.begin();
			em.persist(adminPayment);
		}catch(Exception e){
			System.err.println("异常问题");
			e.printStackTrace();
		}
		finally {
			trans.commit();
		}
	}
	
	/**
	 * 修改支付方式.
	 * 
	 * @param member
	 */
	public void updatePayment(AdminPayment adminPayment){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try {
			trans.begin();
			em.merge(adminPayment);
		}catch(Exception e){
			System.err.println("异常问题");
			e.printStackTrace();
		}
		finally {
			trans.commit();
		}
	}
	
	/**
	 * 删除支付方式.
	 * 
	 * @param payId
	 */
	public void delPayment(Long payId){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try{
			trans.begin();
			AdminPayment payment = em.find(AdminPayment.class, payId);
			if(payment != null){
				em.remove(payment);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			trans.commit();
		}
	}
	
	/**
	 * 获取支付方式.
	 * 
	 * @param memberId
	 * @return
	 */
	public AdminPayment getPayment(Long payId){
		EntityManager em = EMF.getEM();
		AdminPayment payment = null;
		try{
			payment = em.find(AdminPayment.class, payId);
		}catch(Exception e){
			e.printStackTrace();
		}
		return payment;
	}
	
	/**
	 * 支付方式列表.
	 * 
	 * @param page
	 * @return
	 */
	public List<AdminPayment> listPayment(Page page){
		EntityManager em = EMF.getEM();
		Query query = em.createQuery("select from AdminPayment payment where payment.enable = 1 ");
		page.setTotalCount(query.getResultList().size());
		query.setFirstResult(page.getFirst());
		query.setMaxResults(page.getPageSize());
		List<AdminPayment> list = query.getResultList();
		return list;
	}

}
